package 动态规划;

public class 区域矩阵和 {
    public int[][] matrixBlockSum(int[][] mat, int k) {
        int m = mat.length;
        int n = mat[0].length;
        int[][] res = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int sum = 0;
                for (int l = Math.max(0,i-k); l <=Math.min(i+k,m-1) ; l++) {
                    for (int o = Math.max(0,j-k); o <= Math.min(j+k,n-1); o++) {
                        sum+=mat[l][o];
                    }
                }
                res[i][j] = sum;
            }
        }
        return res;
    }
}
